/*
 * envset.c
 *
 *  Created on: 2013/02/14
 *      Author: tuwei
 */
#include <stdio.h>
#include <string.h>
#include "typedef.h"
#include "conf.h"
#include "env.h"

/* Server IP */
char g_server_ip[MAX_PATH] = {0};
/* Server Port */
int g_port = 9000;
/* CPU Number */
int g_local_cpus = 0;
/* Log file dir path */
char g_log_folder_path[MAX_PATH] = {0};
/* reconnect interval time */
int g_reconn_interval_time = 60;
/* flush time */
int g_flush_time = 10;

/**
 * Initialize the env setting.
 */
int env_init()
{
	int ret = SUCCESS;
	/* If configeration initialize is failed, use all the
	   default values. */
	if (cfg_init() != SUCCESS)
	{
		ret = ERROR;
	}
	else
	{
		// get the server ip address.
		if (cfg_getstring("server_ip", g_server_ip, MAX_PATH) != SUCCESS)
		{
			strcpy(g_log_folder_path, "NONE");
		}

		// get port number
		int port = cfg_getnumber("port_default");
		if (port != -1)
		{
			g_port = port;
		}

		// get cpu number
		int cpus = cfg_getnumber("local_cpus_number");
		if (cpus != -1)
		{
			g_local_cpus = cpus;
		}

		// get log file folder path.
		if (cfg_getstring("log_folder_path", g_log_folder_path, MAX_PATH) != SUCCESS)
		{
			strcpy(g_log_folder_path, "/var/log");
		}

		// get reconnection interval time.
		int interval = cfg_getnumber("reconn_interval_time");
		if (interval != -1)
		{
			g_reconn_interval_time = interval;
		}
		// get flush time.
		int flush = cfg_getnumber("flush_time");
		if (flush != -1)
		{
			g_flush_time = flush;
		}

		// release the config data.
		cfg_release();
	}

	return ret;
}

